<?php 
class UserController extends Zend_Controller_Action
{
    public function init()
    {
    }
    public function preDispatch() 
    {
        $auth = Zend_Auth::getInstance();
        if (!$auth->hasIdentity()) {
            if ($this->_request->getActionName() != 'login') {
                $this->_redirect('/user/login');
            }
        }
    }
    
    public function indexAction() 
    {
		$auth  = Zend_Auth::getInstance(); 
		$infoUser = $auth->getIdentity();
		$this->view->username  = $infoUser->username;
	}

    public function usernameAction()
    {          
        $user_mapper = new Application_Model_User();   
        if($params=$this->getRequest()->getParams())
        {
            $user =$params['txtuname'];
            $users = $user_mapper->fetchAll(array('username = ?'=>$user)); 
            if(count($users)>0)
            {
                $this->view->userExist ='This username has already exist. Please register by another email';
            } 
            else
            {
                $this->view->userExist ='This email is available';
            }
        }
    }
    
    public function registerAction()
    {
        try {
            $request = $this->getRequest();
            if($request->isPost())
            {
                $user = new Application_Model_User();
                $user->setUsername($request->get('txtuname'));
                $user->setEmail($request->get('txtEmail'));
                $user->setPassword(md5($request->get('txtPassword')));
                $user->save();
                $this->_redirect('/user/login');
                }
            }
        catch(exception $ex)
        {
            print_r($ex);
        }   
    }
    public function loginAction()
    {
        try
        {
            if ($this->_request->isPost()) {
    
            
            $db = Zend_Registry::get('connectDB');
            
            $auth = Zend_Auth::getInstance ();
            
            //3. Khai bao bang va 2 cot se su dung so sanh trong qua trinh login
            $authAdapter = new Zend_Auth_Adapter_DbTable($db);
            $authAdapter->setTableName('user')
                    ->setIdentityColumn('username')
                    ->setCredentialColumn('password');
            
            //4. Lay gia tri duoc gui qua tu FORM
            $uname= $this->_request->getParam('username');
            $paswd = $this->_request->getParam('password');
               
            //5. Dua vao so sanh voi du lieu khai bao o muc 3
            $authAdapter->setIdentity($uname);
            
            $authAdapter->setCredential(md5($paswd)); 
            //6. Kiem tra trang thai cua user neu status = 1 moi duoc login
            $select = $authAdapter->getDbSelect();
            $select->where('status = 1');
            //7. Lay ket qua truy van
            $result = $auth->authenticate($authAdapter);
            $flag = 0;
            if ($result->isValid()) 
            {
                //8. Lay nhung du lieu can thiet trong bang users neu login thanh cong				
                $data = $authAdapter->getResultRowObject(null, array('password'));
                
                //9. Luu  nhung du lieu cua member vao session
                $auth->getStorage()->write($data);
                
                //d($data);die;
                $flag = 1;
                
                if($data->role == 1)
                {
                    $flag = 2;
                }
            }
            else
            {
                echo "Email of Password not correct";
            }
            
            if ($flag == 1 )
            {
                
                $this->_redirect('/');                    
            }
            else if($flag == 2)
            {
                $this ->_redirect('/admin');
            }
            
        } 
    }
    catch(exception $ex){
            echo '<pre>';
            print_r($ex);
            die;
        }                      
    } 
    
    public function logoutAction()
    {
        $auth = Zend_Auth::getInstance();
        $auth->clearIdentity();
        $this->_redirect('/');  
    } 
}
?>